local skynet = require "skynet"
local filename = "slotmgr.lua"
local filelog = require "filelog"
local queue = require "skynet.queue"
local base = require "base"
local redisdao = require "redisdao"
local tabletool = require "tabletool"
local json = require "cjson"
json.encode_sparse_array(true,1,1)

local SlotTool = require "slottool"

local SERVICE_FEE_RATE = 0.035

--系统输钱上限基值,默认10万
local SystemLoseLimit_Base = 100000

--系统输钱上限控制系数
local SystemLoseLimit_Factor = 0.08

local CMD = {}
--
--处理结算结果
--@total_change 总变化值
--@total_water  总流水值
--@total_bet    玩家总下注
--
function CMD.handle_balance_result(gamename, total_change, total_water, total_bet, comment, baseval)
    -- filelog.trace("handle_balance_result", gamename, total_change, total_water, total_bet, comment)
    local tax_rate = SlotTool.get_tax_rate_from_redis(gamename)
    local profit_val = math.floor(total_bet * tax_rate * 100) / 100
    local inventory_val = -total_change

    --更新收益值
    local result1 = SlotTool.update_system_profit(gamename, profit_val, comment)
    if result1 then
        inventory_val = inventory_val - profit_val
    end

    --更新库存值，扣除抽水
    local service_fee_rate = CMD.get_service_fee_rate(gamename, baseval)    -- SlotTool.get_service_fee_rate_from_redis(gamename)
    local service_fee_val = math.floor(total_water * service_fee_rate * 100) / 100
    inventory_val = inventory_val - service_fee_val

    return SlotTool.update_system_inventory(gamename, inventory_val, comment)
end

--
--从redis获取系统库存值
--
function CMD.get_inventory_from_redis(gamename)
    return SlotTool.get_static_inventory_from_redis(gamename)
end

--
--获取抽水率
--
function CMD.get_service_fee_rate(gamename, baseval)
    if baseval == 10 then return 0 end
    return SERVICE_FEE_RATE -- SlotTool.get_service_fee_rate_from_redis(gamename)
end

--
--设置系统输钱上限
--
function CMD.set_system_lose_limit(loselimit_base, loselimit_factor)
    loselimit_base = loselimit_base or 0
    loselimit_factor = loselimit_factor or 0.02
    if loselimit_base < 0 or loselimit_factor < 0 then
        return false
    end
    SystemLoseLimit_Base = loselimit_base
    SystemLoseLimit_Factor = loselimit_factor

    return true
end

--
--获得系统输钱上限
--
function CMD.get_system_lose_limit(gamename)
    local loselimit = 0
    local S = CMD.get_service_income(gamename)
    if S < 0 then
        loselimit = SystemLoseLimit_Base
    else
        loselimit = math.floor(S * SystemLoseLimit_Factor) + SystemLoseLimit_Base
    end

    return loselimit
end

--
--更新系统收入（机器人，总收入，不是提取）
--
function CMD.update_sys_income(gamename, total_income, comment)
    -- filelog.trace("update_sys_income", gamename, total_income, comment)
    SlotTool.update_system_income(gamename, total_income, comment)

    local income = CMD.get_service_income(gamename) or 0
    local loselimit = CMD.get_system_lose_limit(gamename) or 0
    -- filelog.debug("update_sys_income, now income = " .. income, "now loselimit = " .. loselimit)
    if income <= -loselimit then
        skynet.send(".router", "lua", "set_game_robot_state", false)
    end
end

--
--从redis获取系统收入值（主要是参与游戏的机器人的收入值，总收入，不是提取，可正可负）
--
function CMD.get_service_income(gamename)
    return SlotTool.get_income_from_redis(gamename)
end

-------------------
--
skynet.start(function()
    skynet.dispatch("lua",function(session, source, cmd, ...)
        local f = CMD[cmd]
        if f then
            skynet.retpack(f(...))
        else
            skynet.retpack()
        end
    end)
end)